package cn.com.xiaoshoujia.quartz.site;

import cn.com.xiaoshoujia.activities.dto.ActivityRefundSmsDTO;
import cn.com.xiaoshoujia.application.service.SMSService;
import cn.com.xiaoshoujia.domain.enums.SignName;
import cn.com.xiaoshoujia.order.mapper.OrderMapper;
import cn.com.xiaoshoujia.order.mapper.RefundMapper;
import cn.com.xiaoshoujia.user.mapper.ActivityTeamHeadMapper;
import cn.com.xiaoshoujia.utils.SerializationUtils;
import org.apache.commons.lang.StringUtils;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityQuartz implements Job {

    private Logger logger = LoggerFactory.getLogger(ActivityQuartz.class);

    @Autowired
    private ActivityTeamHeadMapper activityTeamHeadMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RefundMapper refundMapper;

    @Autowired
    private SMSService smsService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDataMap dataMap = context.getJobDetail().getJobDataMap();
        Long id = dataMap.getLong("id");
        logger.info("team site quartz id {}", id);
        /*根据活动唯一ID更改head表内所有未成功团状态为已失败*/
        int i = 0;
        List<String> list = orderMapper.findByActivityId(id);
        if (list != null && list.size() > 0) {
            i = refundMapper.save(list);
        }
        if (i > 0) {
            List<Long> headIdList = activityTeamHeadMapper.findHeadByActivityId(id);
            logger.info("headIdList is {}",headIdList);
            activityTeamHeadMapper.updateStatusByActivityId(id);
            int cou = orderMapper.updateStatusByActivityId(id);
            logger.info("order no pay count = {}",cou);
            if (null!=headIdList&&headIdList.size()>0){
                logger.info("headIdList have size");
                for (Long headId : headIdList) {
                    logger.info("headId is {}",headId);
                    List<ActivityRefundSmsDTO> refundSmsDTOList = activityTeamHeadMapper.findHeadFail(headId);
                    if (null!=refundSmsDTOList&&refundSmsDTOList.size()>0){
                        logger.info("send msg {}",refundSmsDTOList);
                        Map<String, String> map = new HashMap<>();
                        map.put("name", StringUtils.substring(refundSmsDTOList.get(0).getTitle(), 0, 10));
                        String phone = refundSmsDTOList.get(0).getPhone();
                        if (StringUtils.isNotBlank(phone)){
                            String[] strs = SerializationUtils.getJsonArrStrFormStrings(phone);
                            if (null != strs)
                                map.put("contact",StringUtils.join(strs, ",") );
                            else
                                map.put("contact","--" );
                        }else{
                            map.put("contact","--" );
                        }
                        map.put("comname", StringUtils.substring(refundSmsDTOList.get(0).getSponsor(), 0, 10));
                        for (ActivityRefundSmsDTO activityRefundSmsDTO : refundSmsDTOList) {
                            logger.info("send message",activityRefundSmsDTO.getCluesPhone());
                            String s = smsService.sendMessage(activityRefundSmsDTO.getCluesPhone(), SignName.SHAREBANG, "SMS_147437983", map);
                            logger.info("back message {}",s);
                        }
                    }
                }
            }
        }

    }
}
